<?php
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/1/2014
 * Time: 9:40 μμ
 */

include('admin_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */


if ($_POST['click'] == "1")
{
    $sql_imba = "SHOW COLUMNS FROM field_data LIKE 'var%'";
    $result_imba = mysqli_query($con, $sql_imba);
    $i=0;
    $sql_imba_ = 'INSERT INTO field_data (`id`,`auth_id`,`date_create`, `date_update`,`action_id`,`field_category_action_id`';

    $pvalues ="";
    while($row_imba = mysqli_fetch_array($result_imba))
    {
        $sql_imba_ = $sql_imba_.',`'.$row_imba['Field'].'`';
        $imba = '"'.$row_imba['Field'].'"';
        //echo $imba.'|'.$_POST[$imba].'<br>';
        if (stripos($row_imba['Type'],"var") !== false)
        {
            $pvalues = $pvalues.',"'.$_POST[$row_imba['Field']].'"';
        }
        if (stripos($row_imba['Type'],"int") !== false)
        {
            $pvalues = $pvalues.','.intval($_POST[$row_imba['Field']]);
        }
        if (stripos($row_imba['Type'],"blob") !== false)
        {
            if($_FILES[$row_imba['Field']]['size'] > 0)
            {
                //$fileName = $_FILES['userfile']['name'];
                $tmpName  = $_FILES[$row_imba['Field']]['tmp_name'];
                //$fileSize = $_FILES['userfile']['size'];
                //$fileType = $_FILES['userfile']['type'];

                $fp      = fopen($tmpName, 'r');
                $content = fread($fp, filesize($tmpName));
                $content = addslashes($content);
                fclose($fp);
            }

            $pvalues = $pvalues.',\''.$content.'\'';
        }

    }
    $sql_imba_ = $sql_imba_ . ') VALUES (NULL,'.$authid.',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),'.intval($_POST['action_id']).','.intval($_POST['fcai']);
    $sql_imba_ = $sql_imba_. $pvalues.')';
    mysqli_query($con,$sql_imba_) or die('Error, query failed!!!');

    /*
    $src = "noum";

    for ($i=0; $i<=36; $i++) {

        $sql_imba_ = str_replace($src,"no ".$i, $sql_imba_);
        // echo $sql_imba_.'<br>';
        mysqli_query($con,$sql_imba_) or die('Error, query failed!!!');
        $src = "no ".$i;

    }
*/
    //$result_imba_ =  mysqli_query($con, $sql_imba_);
    echo 'Προσθήκη αγγελίας...';
}